/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.gui.salle;

import java.awt.Dimension;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import fr.univ_tln.inf9.exaplanning.api.salle.Material;
import fr.univ_tln.inf9.exaplanning.api.salle.Room;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListRoomChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.Position_xChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.Position_yChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.RoomAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.RoomRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.controleur.salle.BuildingController;
import fr.univ_tln.inf9.exaplanning.controleur.salle.BuildingView;
import fr.univ_tln.inf9.exaplanning.controleur.salle.RoomController;


/**
 * @author pivi
 *
 */
public class JFrameListRoom extends BuildingView implements ActionListener  {

	private JFrame frame = null;
	private JPanel contentPane = null;
	private JList listRoom = null;
	private JScrollPane scrollRoom = null;
	private DefaultListModel jListModel = null;  //  @jve:decl-index=0:visual-constraint="39,48"
	private static List<String> listzero = null;
	private String nameBuild = null;

	private TextField textField = null;
	
	private JButton addButton;
	private JButton removeButton;
	
	public JFrameListRoom(BuildingController controller) {
		this(controller, listzero,null);
	}
	
	public JFrameListRoom(BuildingController controller, List<String> r,String nameBuild){
		super(controller); 
		this.nameBuild = nameBuild;
		buildFrame(r);
	}
	
	private void buildFrame(List<String> r) {
		frame = new JFrame();
		
		contentPane = new JPanel();
		this.contentPane.setSize(20,30);
		
		jListModel = new DefaultListModel();
		
		/**
		 * Insertion des éléments
		 */
		Iterator<String> it = r.iterator(); 
		while(it.hasNext()) {
			jListModel.addElement(it.next());
		}
		
		listRoom = new JList(jListModel);
		
		scrollRoom = new JScrollPane(listRoom);
		contentPane.add(scrollRoom);
		
		this.addButton = new JButton("ajouter salle");
		this.addButton.addActionListener(this);
		
		this.removeButton = new JButton("enlever salle");
		this.removeButton.addActionListener(this);
		
		textField = new TextField("Votre ajout");
		textField.setSize(new Dimension(50,10));
		contentPane.add(textField);
		contentPane.add(removeButton);
		contentPane.add(addButton);
		
		
		frame.setContentPane(contentPane);
		frame.setTitle("Salle du batîment "+nameBuild);
		frame.pack();
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("ajouter salle")){
			Room r = new Room((String)this.textField.getText(),null);
			this.getController().notifyRoomAddedToList(r);	
		}
		if(arg0.getActionCommand().equals("enlever salle")){
		this.getController().notifyRoomRemovedToList((String)this.listRoom.getSelectedValue());
		}
		
	}

	
	
	
	@Override
	public void ListRoomChanged(ListRoomChangedEvent event) {
		jListModel.addElement(event.getNewListRoom());
		
	}

	@Override
	public void RoomAddedToList(RoomAddedToListEvent event) {
		jListModel.addElement(event.getNewRoom());
		
	}

	@Override
	public void RoomRemovedToList(RoomRemovedToListEvent event) {
		jListModel.removeElement(event.getRoomToRemove());
		
	}

	@Override
	public void close() {
		frame.dispose();
	}

	@Override
	public void display() {
		frame.setVisible(true);
	}

	@Override
	public void Position_xChanged(Position_xChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Position_yChanged(Position_yChangedEvent event) {
		// TODO Auto-generated method stub
		
	}



}
